home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Blender 2.49b / blender-2.49b-windows.exe / $_4_ / .blender / scripts / weightpaint_average.py < prev    next >
Text File  |  2009-08-31  |  4KB  |  121 lines

  1. #!BPY
  2. """
  3. Name: 'Vertex Groups Island Average'
  4. Blender: 243
  5. Group: 'WeightPaint'
  6. Tooltip: 'Average the vertex weights for each connected set of verts'
  7. """
  8.  
  9. # -------------------------------------------------------------------------- 
  10. # ***** BEGIN GPL LICENSE BLOCK ***** 
  11. # This program is free software; you can redistribute it and/or 
  12. # modify it under the terms of the GNU General Public License 
  13. # as published by the Free Software Foundation; either version 2 
  14. # of the License, or (at your option) any later version. 
  15. # This program is distributed in the hope that it will be useful, 
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of 
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  18. # GNU General Public License for more details. 
  19. # You should have received a copy of the GNU General Public License 
  20. # along with this program; if not, write to the Free Software Foundation, 
  21. # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
  22. # ***** END GPL LICENCE BLOCK ***** 
  23. # --------------------------------------------------------------------------
  24. import Blender
  25. from Blender import Scene, Mesh, Window, sys, Draw
  26. from BPyMesh import meshWeight2List, list2MeshWeight, mesh2linkedFaces
  27.  
  28. import BPyMessages
  29. import bpy
  30.  
  31. def faceGroups2VertSets(face_groups):
  32.     '''    Return the face groups as sets of vert indicies    '''
  33.     return [set([v.index for f in fg for v in f]) for fg in face_groups]
  34.  
  35.  
  36. def vgroup_average(ob_orig, me, sce, PREF_ALL_VGROUPS=True):
  37.     if not me.getVertGroupNames():
  38.         return
  39.     
  40.     weight_names, weight_list = meshWeight2List(me)
  41.     
  42.     weight_names_len = len(weight_names)
  43.     vgroup_dummy = [0.0] * weight_names_len
  44.     vgroup_range = range(weight_names_len)
  45.     
  46.     if not PREF_ALL_VGROUPS:
  47.         weight_active_index = weight_names.index(me.activeGroup)
  48.     
  49.     for vert_set in faceGroups2VertSets( mesh2linkedFaces(me) ):
  50.         if not vert_set:
  51.             continue
  52.         
  53.         
  54.         if PREF_ALL_VGROUPS:
  55.             # We need to average the vgroups
  56.             collected_group = vgroup_dummy[:]
  57.             for i in vert_set:
  58.                 vert_group = weight_list[i]            # get the original weight
  59.                 weight_list[i] = collected_group    # replace with the collected group
  60.                 
  61.                 for j in vgroup_range: # iter through the vgroups
  62.                     collected_group[j] += vert_group[j]
  63.             
  64.             for j in vgroup_range:
  65.                 collected_group[j] /= len(vert_set)
  66.         else:
  67.             # Active group only
  68.             vert_weight = 0.0
  69.             for i in vert_set:
  70.                 vert_weight += weight_list[i][weight_active_index]
  71.             
  72.             vert_weight /= len(vert_set)
  73.             
  74.             for i in vert_set:
  75.                 weight_list[i][weight_active_index] = vert_weight
  76.     
  77.     list2MeshWeight(me, weight_names, weight_list)
  78.  
  79. def main():
  80.     
  81.     # Gets the current scene, there can be many scenes in 1 blend file.
  82.     sce = bpy.data.scenes.active
  83.     
  84.     # Get the active object, there can only ever be 1
  85.     # and the active object is always the editmode object.
  86.     ob_act = sce.objects.active
  87.     
  88.     if not ob_act or ob_act.type != 'Mesh':
  89.         BPyMessages.Error_NoMeshActive()
  90.         return 
  91.     
  92.     # Saves the editmode state and go's out of 
  93.     # editmode if its enabled, we cant make
  94.     # changes to the mesh data while in editmode.
  95.     is_editmode = Window.EditMode()
  96.     Window.EditMode(0)
  97.     
  98.     PREF_ALL_VGROUPS = Draw.PupMenu("All Groups?%t|All Groups%x1|Active Group Only%x0")
  99.     if PREF_ALL_VGROUPS==-1:
  100.         return
  101.     
  102.     Window.WaitCursor(1)
  103.     me = ob_act.getData(mesh=1) # old NMesh api is default
  104.     t = sys.time()
  105.     
  106.     # Run the mesh editing function
  107.     vgroup_average(ob_act, me, sce, PREF_ALL_VGROUPS)
  108.     
  109.     # Timing the script is a good way to be aware on any speed hits when scripting
  110.     print 'Average VGroups in %.2f seconds' % (sys.time()-t)
  111.     Window.WaitCursor(0)
  112.     if is_editmode: Window.EditMode(1)
  113.     
  114.     
  115. # This lets you can import the script without running it
  116. if __name__ == '__main__':
  117.     main()